﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SuperStore.Models;

namespace SuperStore.Controllers
{
    public class ProfileController : SuperStoreController
    {
        public ActionResult Join()
        {
            if (this.ValidMemberID)
            {
                ViewData.Add("Status Message", "Already Logged In");
                return RedirectToAction("Index", "Home");
            }

            return View();
        }

        [HttpPost]
        public ActionResult Join(string emailAddress, string password)
        {
            try
            {
                Member member = new Member();
                member.EmailAddress = emailAddress;
                member.PasswordHash = password.Hash();
                member.PK = Guid.NewGuid();
                Database.Instance.InsertMember(member);

                var newCookie = new HttpCookie("MemberID", member.PK.ToString());
                newCookie.Expires = DateTime.Now.AddDays(10);
                Response.AppendCookie(newCookie);

                ViewData.Add("Status Message", "Congrats, you have joined!");
                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Update()
        {
            if (string.IsNullOrWhiteSpace(this.MemberID))
            {
                return RedirectToAction("Login", "Profile");
            }

            Member member = (from m in Database.Instance.Members
                             where m.PK.Equals(new Guid(this.MemberID))
                             select m).Single();

            return View(member);
        }


        [HttpPost]
        public ActionResult Update(string emailAddress, string password)
        {
            try
            {
                Member member = (from m in Database.Instance.Members
                                 where m.PK.Equals(new Guid(this.MemberID))
                                 select m).Single();

                member.EmailAddress = emailAddress;
                member.PasswordHash = password.Hash();

                Database.Update(member);

                ViewData.Add("Status Message", "Details updated!");

                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Director()
        {
            if (this.ValidMemberID)
            {
                return RedirectToAction("Update");
            }
            else
            {
                RemoveCookie();
                return RedirectToAction("Login");
            }
        }

        public ActionResult Login()
        {
            return View();
        }

        public ActionResult Logout()
        {
            RemoveCookie();
            ViewData.Add("Status Message", "Logged Out");
            return RedirectToAction("Index", "Home");
        }

        private void RemoveCookie()
        {
            if (Response.Cookies["MemberID"] != null)
            {
                Response.Cookies["MemberID"].Expires = DateTime.Now.AddDays(-1);
            }
        }

        [HttpPost]
        public ActionResult Login(string emailAddress, string password)
        {
            try
            {
                Member member = (from m in Database.Instance.Members
                                 where m.EmailAddress.Equals(emailAddress, StringComparison.CurrentCultureIgnoreCase) &&
                                 m.PasswordHash.Equals(password.Hash())
                                 select m).Single();

                var newCookie = new HttpCookie("MemberID", member.PK.ToString());
                newCookie.Expires = DateTime.Now.AddDays(10);
                Response.AppendCookie(newCookie);

                ViewData.Add("Status Message", "Congrats, you have logged in!");

                return RedirectToAction("Index", "Home");
            }
            catch
            {
                ViewData.Add("Status Message", "Incorrect details or you are not a member!");
                return View();
            }
        }
    }
}
